package com.adminsystem.controller;

import com.adminsystem.common.utils.StringUtils;
import com.adminsystem.entity.*;
import com.adminsystem.entity.custom.LoggerPageBean;
//import com.adminsystem.entity.custom.R;
import com.adminsystem.service.SysLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.core.http.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "日志类")
@RestController
@RequestMapping("/sys/logger")
public class SysLoggerController {

    @Autowired
    private SysLogService sysLogService;
    @ApiOperation("获取日志信息")
    @ApiParam("请求参数")
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('system:logger:list')")
    public R list(@RequestBody LoggerPageBean loggerPageBean) {
        String query = loggerPageBean.getQuery().trim();
        String content=loggerPageBean.getContent().trim();
        Page<SysLog> pageResult = sysLogService.page(
                new Page<>(loggerPageBean.getPageNum(),
                        loggerPageBean.getPageSize()),
                new QueryWrapper<SysLog>()
                        .like(StringUtils.isNotEmpty(query), "username", query)
                        .like(StringUtils.isNotEmpty(content), "content", content)
                        .orderByDesc("create_time"));
        List<SysLog> roleList = pageResult.getRecords();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("loggerList", roleList);
        resultMap.put("total", pageResult.getTotal());
        return R.success(resultMap);
    }
}
